
#include "kb.h"                 // keybaord event type 
#include "touch_algorithm.h"    // touch event type 
#include "custom_event.h"
#include "gui.h"                // GUI header

#include "views.h"              // include all views 
#include "views_util.h"
#include "log.h"                // LOG file 
#define LOG     LOG_INFO0

#include "xapp_config.h"
#include "xapp_clock.h"
#include "xapp_exercise.h"

#if USER_CHECK_ENABLED( HRS )
#include "xapp_hrs.h"
#endif 

#include "xapp_localize.h"



enum tagExerciseRecord
{
    tagExerciseRecordTipExit,
    tagExerciseRecordLastView,
};

enum idxIcon
{
    idxIconStep,
    idxIconDistance,
    idxIconCalorie,
    idxIconHeartrate,
    idxIconHeartrateSmall,
    idxIconPace,
};

enum bitFlag
{
    bitFlagTimeUpdate,
    bitFlagValueUpdate,
    bitFlagScrollUpdate,
};

enum flagDataRequired
{
    flagDataRequiredAll, // heartrate/pace/calorie/distance/steps
    flagDataRequiredHCDS, //heartrate/caloire/distance/steps
    flagDataRequiredHCD, //heartrate/calorie/distacne
    flagDataRequiredHC, // heartrate/calorie
};

enum idxSportIcon
{
    idxSportIconRunning,
    idxSportIconWalkingRace,
    idxSportIconPushup,
    idxSportIconCrunches,
    idxSportIconRiding,
    idxSportIconClimbing,
    idxSportIconSwimming,
    idxSportIconRopeskipping,
    idxSportIconHula,
    idxSportIconBasketball,
    idxSportIconVolleyball,
    idxSportIconPingpong,
    idxSportIconBadminton,
    idxSportIconFootball,
};

// declare function 
GUI_VIEW_FUNC_DECLARE_ALL;
static void local_view_event_handler( uint32_t tag );

typedef struct 
{
    uint8_t type;
    uint8_t icon;
    uint16_t tag; 
}sport_item_list_t; 
static const sport_item_list_t  sport_item_list[] = 
{
    { TASK_EXERCISE_TYPE_ROPESKIPPING,  idxSportIconRopeskipping,   TEXT_TAG_EXERCISE_ROPESKIPPING }, // 心率、卡路里
    { TASK_EXERCISE_TYPE_RUNNING,       idxSportIconRunning,        TEXT_TAG_EXERCISE_RUNNING }, // 步数、卡路里、距离、心率、配速  
    { TASK_EXERCISE_TYPE_WALKINGRACE,   idxSportIconWalkingRace,    TEXT_TAG_EXERCISE_WALKING_RACE }, // 步数、卡路里、距离、心率
    { TASK_EXERCISE_TYPE_CLIMBING,      idxSportIconClimbing,       TEXT_TAG_EXERCISE_CLIMBING }, // 卡路里、距离、心率
    { TASK_EXERCISE_TYPE_VOLLEYBALL,    idxSportIconVolleyball,     TEXT_TAG_EXERCISE_VOLLEY_BALL }, // 卡路里，距离、心率
    { TASK_EXERCISE_TYPE_BASKETBALL,    idxSportIconBasketball,     TEXT_TAG_EXERCISE_BAKSET_BALL }, // 卡路里、距离、心率
    { TASK_EXERCISE_TYPE_FOOTBALL,      idxSportIconFootball,       TEXT_TAG_EXERCISE_FOOTBALL }, // 卡路里、距离、心率
    { TASK_EXERCISE_TYPE_BADMINTON,     idxSportIconBadminton,      TEXT_TAG_EXERCISE_BAMINTON }, // 卡路里、距离、心率
    { TASK_EXERCISE_TYPE_PINGPONG,      idxSportIconPingpong,       TEXT_TAG_EXERCISE_PINGPONG }, // 卡路里、距离、心率
};

static uint8_t string_time[6];
static uint8_t string_value_0[10];
static uint8_t string_value_1[10];
static uint8_t string_value_2[10];
static uint8_t string_value_3[10];
static uint8_t string_value_4[10];
static uint8_t string_exercise_time[10];

const char format_steps[] = "%d";
const char format_calorie[] = "%d";
const char format_distance[] = "%1.2f";
const char format_heartrate[] = "%d";
const char format_speed[] = "%d'%02d\"";

// 心率、配速、卡路里、里程、步数
const static ui_widgets_type_t wglExerciseRecordHPCDS/*1. modify name*/[] = 
{
    // set background black 
    [0] = uiCanvas(0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ),

    // 时间 
    [1] = uiNumberlistWithBuffer( SIZE_SCALE(90), SIZE_SCALE(0), SIZE_SCALE(60), SIZE_SCALE(26), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d:%02d", string_time, 6),

    [2] = uiListBox(0, SIZE_SCALE(30), GUI_LCD_RES_H, GUI_LCD_RES_V - SIZE_SCALE(30), 18, 0, 0, false),
    // 图标
    [3] = uiDigital( IMG_Exercise_Type_Icon_075_075_14, SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(50), SIZE_SCALE(50) ),
    // text
    [4] = uiTextbox( SIZE_SCALE(70), SIZE_SCALE(0), GUI_LCD_RES_H - SIZE_SCALE(90), SIZE_SCALE(50), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE,  ENCODING_UNICODE, FONT_UNICODE_20_29 ),    
    
    // time elapsed 
    [5] = uiNumberlistWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0 + 50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), COLOR_BLACK, COLOR_CYAN, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "%02d:%02d:%02d", string_exercise_time, 10),

    // heartrate
    [6] = uiStack( SIZE_SCALE(30), SIZE_SCALE(40) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), 2 ),
    [7] = uiPictureBox( IMG_Exercise_Data_Icon_036_036_06, idxIconHeartrate, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24) ),
    [8] = uiNumberWithBuffer( SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d", string_value_0, 8),
    // pace
    [9] = uiStack(SIZE_SCALE(30), SIZE_SCALE(80) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40) , 2),
    [10] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconPace, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [11] = uiNumberlistWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d' %02d\"", string_value_1, 8),
    // calorie
    [12] = uiStack(SIZE_SCALE(30), SIZE_SCALE(120) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), 2),
    [13] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconCalorie, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [14] = uiNumberWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%d kcal", string_value_2, 8),
    // distance 
    [15] = uiStack(SIZE_SCALE(30), SIZE_SCALE(160) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), 2),
    [16] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconDistance, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [17] = uiNumberWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%1.2f km", string_value_3, 8),
    //  steps
    [18] = uiStack(SIZE_SCALE(30), SIZE_SCALE(200) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), 2),
    [19] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconStep, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [20] = uiNumberWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%d", string_value_4, 8),

};
// 心率、卡路里、里程、步数
const static ui_widgets_type_t wglExerciseRecordHCDS/*1. modify name*/[] =
{
    // set background black 
    [0] = uiCanvas(0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK),

    // 时间 
    [1] = uiNumberlistWithBuffer(SIZE_SCALE(90), SIZE_SCALE(0), SIZE_SCALE(60), SIZE_SCALE(26), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d:%02d", string_time, 6),

    [2] = uiListBox(SIZE_SCALE(0), SIZE_SCALE(30), GUI_LCD_RES_H, GUI_LCD_RES_V - SIZE_SCALE(30), 15, 0, 0, false),

    // 图标
    [3] = uiDigital(IMG_Exercise_Type_Icon_075_075_14, SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(50), SIZE_SCALE(50)),
    // text
    [4] = uiTextbox(SIZE_SCALE(70), SIZE_SCALE(0), GUI_LCD_RES_H - SIZE_SCALE(90), SIZE_SCALE(50), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE,  ENCODING_UNICODE, FONT_UNICODE_20_29),

    
    // time elapsed 
    [5] = uiNumberlistWithBuffer(SIZE_SCALE(30), 0 + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), COLOR_BLACK, COLOR_CYAN, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "%02d:%02d:%02d", string_exercise_time, 10),

    // heartrate
    [6] = uiStack(SIZE_SCALE(30), SIZE_SCALE(40) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), 2),
    [7] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconHeartrate, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [8] = uiNumberWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d", string_value_0, 8),
    // calorie
    [9] = uiStack(SIZE_SCALE(30), SIZE_SCALE(80) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40) , 2),
    [10] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconCalorie, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [11] = uiNumberWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%d kcal", string_value_2, 8),
    // distance
    [12] = uiStack(SIZE_SCALE(30), SIZE_SCALE(120) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), 2),
    [13] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconDistance, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [14] = uiNumberWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%1.2f km", string_value_3, 8),
    // steps
    [15] = uiStack(SIZE_SCALE(30), SIZE_SCALE(160) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), 2),
    [16] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconStep, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [17] = uiNumberWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%d", string_value_4, 8),



};
// 心率、卡路里、里程
const static ui_widgets_type_t wglExerciseRecordHCD/*1. modify name*/[] =
{
    // set background black 
    [0] = uiCanvas(0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK),

    // 时间 
    [1] = uiNumberlistWithBuffer(SIZE_SCALE(90), SIZE_SCALE(0), SIZE_SCALE(60), SIZE_SCALE(26), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d:%02d", string_time, 6),

    [2] = uiListBox(SIZE_SCALE(0), SIZE_SCALE(30), GUI_LCD_RES_H, GUI_LCD_RES_V - SIZE_SCALE(30), 12, 0, 0, false),
    // 图标
    [3] = uiDigital(IMG_Exercise_Type_Icon_075_075_14, SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(50), SIZE_SCALE(50)),
    // text
    [4] = uiTextbox(SIZE_SCALE(70), SIZE_SCALE(0), GUI_LCD_RES_H - SIZE_SCALE(90), SIZE_SCALE(50), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE,  ENCODING_UNICODE, FONT_UNICODE_20_29),
        
    // time elapsed 
    [5] = uiNumberlistWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0 + 50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), COLOR_BLACK, COLOR_CYAN, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "%02d:%02d:%02d", string_exercise_time, 10),

    // heartrate
    [6] = uiStack(SIZE_SCALE(30), SIZE_SCALE(40) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), 2),
    [7] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconHeartrate, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [8] = uiNumberWithBuffer(SIZE_SCALE(30), 0, SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d", string_value_0, 8),
    // calorie
    [9] = uiStack(SIZE_SCALE(30), SIZE_SCALE(80) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40) , 2),
    [10] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconCalorie, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [11] = uiNumberWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%d kcal", string_value_2, 8),
    // distance
    [12] = uiStack(SIZE_SCALE(30), SIZE_SCALE(120) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), 2),
    [13] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconDistance, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [14] = uiNumberWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%1.2f km", string_value_3, 8),

};
// 心率、卡路里
const static ui_widgets_type_t wglExerciseRecordHC/*1. modify name*/[] =
{
    // set background black 
    [0] = uiCanvas(0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK),

    // 时间 
    [1] = uiNumberlistWithBuffer(SIZE_SCALE(90), SIZE_SCALE(0), SIZE_SCALE(60), SIZE_SCALE(26), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d:%02d", string_time, 6),

    [2] = uiListBox(SIZE_SCALE(0), SIZE_SCALE(30), GUI_LCD_RES_H, GUI_LCD_RES_V - SIZE_SCALE(30), 9, 0, 0, false),
    // 图标
    [3] = uiDigital(IMG_Exercise_Type_Icon_075_075_14, SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(50), SIZE_SCALE(50)),
    // text
    [4] = uiTextbox(SIZE_SCALE(70), SIZE_SCALE(0), GUI_LCD_RES_H - SIZE_SCALE(90), SIZE_SCALE(50), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE,  ENCODING_UNICODE, FONT_UNICODE_20_29),

    
    // time elapsed 
    [5] = uiNumberlistWithBuffer(SIZE_SCALE(30), 0 + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), COLOR_BLACK, COLOR_CYAN, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "%02d:%02d:%02d", string_exercise_time, 10),

    // heartrate
    [6] = uiStack(SIZE_SCALE(30), SIZE_SCALE(40) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40), 2),
    [7] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconHeartrate, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [8] = uiNumberWithBuffer(SIZE_SCALE(30), SIZE_SCALE(0), SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d", string_value_0, 8),
    // calorie
    [9] = uiStack(SIZE_SCALE(30), SIZE_SCALE(80) + SIZE_SCALE(50), GUI_LCD_RES_H - SIZE_SCALE(30) * 2, SIZE_SCALE(40) , 2),
    [10] = uiPictureBox(IMG_Exercise_Data_Icon_036_036_06, idxIconDistance, SIZE_SCALE(0),SIZE_SCALE(8), SIZE_SCALE(24), SIZE_SCALE(24)),
    [11] = uiNumberWithBuffer(SIZE_SCALE(30), 0, SIZE_SCALE(130), SIZE_SCALE(40), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%d kcal", string_value_2, 8),
};

GUI_VIEW_DEF_NR( viewExercise_RecordHPCDS ) = 
{
    // name 
    .name = "Exercise+RecordHPCDS",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglExerciseRecordHPCDS /* 3. fill with widgets list name */,
    .widget_cnt = WIDGET_COUNT(wglExerciseRecordHPCDS), /* 3. fill with widgets list name */

    .flag = GUI_VIEW_FLAG_FUNC | GUI_VIEW_FLAG_BG_SUPPORTED
};
GUI_VIEW_DEF_NR(viewExercise_RecordHCDS) =
{
    // name 
    .name = "Exercise+RecordHCDS",

    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func,

    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,

    .pWidgetlist = (ui_widgets_type_t*)wglExerciseRecordHCDS /* 3. fill with widgets list name */,
    .widget_cnt = WIDGET_COUNT(wglExerciseRecordHCDS), /* 3. fill with widgets list name */

    .flag = GUI_VIEW_FLAG_FUNC | GUI_VIEW_FLAG_BG_SUPPORTED
};
GUI_VIEW_DEF_NR(viewExercise_RecordHCD) =
{
    // name 
    .name = "Exercise+RecordHCD",

    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func,

    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,

    .pWidgetlist = (ui_widgets_type_t*)wglExerciseRecordHCD /* 3. fill with widgets list name */,
    .widget_cnt = WIDGET_COUNT(wglExerciseRecordHCD), /* 3. fill with widgets list name */

    .flag = GUI_VIEW_FLAG_FUNC | GUI_VIEW_FLAG_BG_SUPPORTED
};
GUI_VIEW_DEF_NR(viewExercise_RecordHC) =
{
    // name 
    .name = "Exercise+RecordHC",

    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func,

    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,

    .pWidgetlist = (ui_widgets_type_t*)wglExerciseRecordHC /* 3. fill with widgets list name */,
    .widget_cnt = WIDGET_COUNT(wglExerciseRecordHC), /* 3. fill with widgets list name */

    .flag = GUI_VIEW_FLAG_FUNC | GUI_VIEW_FLAG_BG_SUPPORTED
};

static gui_fling_t* mp_exercise_fling;
//static gui_view_t const* p_this_parent = NULL;
static uint8_t flag_data_req = 0;
static sport_item_list_t const *p_exercise_item = NULL;


gui_view_t const* views_load_exercise_record(void)
{
    gui_view_t const* pview;
    const xapp_exercise_desc_t* pdesc = xapp_exercise_get_desc();

    xapp_exercise_type_t type = pdesc->type;
    switch (type)
    {
        case TASK_EXERCISE_TYPE_ROPESKIPPING:// 卡路里、心率
            pview = &viewExercise_RecordHC;
            flag_data_req = flagDataRequiredHC;
            break;
        case TASK_EXERCISE_TYPE_RUNNING:// 心率、配速、卡路里、距离、步数
            pview = &viewExercise_RecordHPCDS;
            flag_data_req = flagDataRequiredAll;
            break;
        case TASK_EXERCISE_TYPE_WALKINGRACE:// 心率、卡路里、距离、步数
            pview = &viewExercise_RecordHCDS;
            flag_data_req = flagDataRequiredHCDS;
            break;
        case TASK_EXERCISE_TYPE_CLIMBING: // 心率、卡路里、里程
        case TASK_EXERCISE_TYPE_VOLLEYBALL:
        case TASK_EXERCISE_TYPE_BASKETBALL:
        case TASK_EXERCISE_TYPE_FOOTBALL:
        case TASK_EXERCISE_TYPE_BADMINTON:
        case TASK_EXERCISE_TYPE_PINGPONG:
            pview = &viewExercise_RecordHCD;
            flag_data_req = flagDataRequiredHCD;
            break;

        default:
            pview = &viewExercise_RecordHPCDS;
            flag_data_req = flagDataRequiredAll;
            break;
    }

    return pview;
}
/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0

*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 1000;
    menu->lifetime = VIEWS_LIFETIME_DEFAULT; 

    mp_exercise_fling = gui_fling_get();
    gui_fling_init_t init_obj = GUI_FLING_CONFIG(0, SIZE_SCALE(30), GUI_LCD_RES_H, GUI_LCD_RES_V-SIZE_SCALE(30), true, 0, 0, SIZE_SCALE(40), 10, NULL);
    gui_fling_init(mp_exercise_fling, &init_obj);

    p_exercise_item = NULL;
    const xapp_exercise_desc_t* p_desc = xapp_exercise_get_desc();
    for(int i = 0; i < sizeof(sport_item_list)/sizeof(sport_item_list[0]); i ++)
    {
        if( sport_item_list[i].type == p_desc->type )
        {
            p_exercise_item = &sport_item_list[i];
            break;
        }
    }
    if( p_exercise_item == NULL )
    {
        p_exercise_item = &sport_item_list[0];
    }
}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{
//    p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{
        
    return GUI_DESTROY_RET_DEFAULT; 
}

/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;

    const datetime_t* pdt = xapp_clock_get_now();
    
    #if USER_CHECK_ENABLED( HRS )
    const xapp_hrs_desc_t* p_hrs_desc = xapp_hrs_get_desc();
    uint8_t bpm = p_hrs_desc->HR.BPM;
    #else 
    uint8_t bpm = 80;
    #endif 
    
    const xapp_exercise_desc_t* p_exercise_desc = xapp_exercise_get_realtime_desc();

    string_t const* s;

    if( IS_REQ_SRC_CREATE( menu->source ) )
    {
        // set fling max-min distance 
        int count = widgets[2].uiStackListBox.extension->stackListbox.count - 4;
        gui_fling_set_min_max(mp_exercise_fling, -40 * count, 0);
                      
        // icon
        PROP_ASSIGN_DIGITAL(prop, p_exercise_item->icon );
        PROP_UPDATE_AT_NO(3);

        // title 
        s = xapp_local_text_item_get( p_exercise_item->tag );
        PROP_ASSIGN_STRING(prop, s);
        PROP_UPDATE_AT_NO(4);

        FLAG_ALL();
    }

    // 定时刷新时间/记录值
    if (menu->source == GUI_VIEW_REQ_SRC_UPDATE_AUTO)
    {
        FLAG_SETB(bitFlagTimeUpdate);
        FLAG_SETB(bitFlagValueUpdate);
    }
    // 滚动过程更新
    if (menu->source == GUI_VIEW_REQ_SRC_UPDATE_FLING)
    {
        PROP_ASSIGN_LISTBOX_OFFSET(prop, mp_exercise_fling->roll_offset_total);
        PROP_UPDATE_AT_NO(2);
    }


    static uint32_t t = 0;
    if( FLAG_GETB( bitFlagTimeUpdate))
    {
        t++;
        // current time 
        PROP_ASSIGN_NUMBERLIST_2(prop, pdt->hour, pdt->minute);
        PROP_UPDATE_AT_NO(1);

        // time elapsed
        PROP_ASSIGN_NUMBERLIST_3(prop, 
                                p_exercise_desc->seconds_of_elapsed / 3600, 
                                p_exercise_desc->seconds_of_elapsed % 3600 / 60, 
                                p_exercise_desc->seconds_of_elapsed % 60
                                );
        PROP_UPDATE_AT_NO(5);
    }
   
    
    if (FLAG_GETB(bitFlagValueUpdate) )
    {
        if( bpm == 0 )
        {
            PROP_UPDATE_NULL_AT_NO( 8 );
        }
        else 
        {
            PROP_ASSIGN_NUMBER(prop, bpm);       
            PROP_UPDATE_AT_NO(8);     
        }
        
        GUI_LOG1("[EXERCISE-VIEW]: flag req = %d\r\n", flag_data_req );
        switch (flag_data_req)
        {
            case flagDataRequiredAll:
                // pace 
                PROP_ASSIGN_NUMBERLIST_2(prop, p_exercise_desc->pace / 60, p_exercise_desc->pace% 60);
                PROP_UPDATE_AT_NO(11);
                // calorie
                PROP_ASSIGN_NUMBER(prop, p_exercise_desc->calorie);
                PROP_UPDATE_AT_NO(14);
                // distance
                PROP_ASSIGN_NUMBER(prop, p_exercise_desc->distance);
                PROP_UPDATE_AT_NO(17);
                // steps
                PROP_ASSIGN_NUMBER(prop, p_exercise_desc->steps);
                PROP_UPDATE_AT_NO(20);
            break;
            case flagDataRequiredHCDS:
                // calorie
                PROP_ASSIGN_NUMBER(prop, p_exercise_desc->calorie);
                PROP_UPDATE_AT_NO(11);
                // distance
                PROP_ASSIGN_NUMBER(prop, p_exercise_desc->distance);
                PROP_UPDATE_AT_NO(14);
                // steps
                PROP_ASSIGN_NUMBER(prop, p_exercise_desc->steps);
                PROP_UPDATE_AT_NO(17);
            break;
            case flagDataRequiredHCD:
                // calorie
                PROP_ASSIGN_NUMBER(prop, p_exercise_desc->calorie);
                PROP_UPDATE_AT_NO(11);
                // distance
                PROP_ASSIGN_NUMBER(prop, p_exercise_desc->distance);
                PROP_UPDATE_AT_NO(14);
            break;
            case flagDataRequiredHC:
                // calorie
                PROP_ASSIGN_NUMBER(prop, p_exercise_desc->calorie);
                PROP_UPDATE_AT_NO(11);
            break;
        }
    }

    FLAG_CLEAR();  
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;
    gui_core_lifetime_refresh(menu);
    switch( touch->type )
    {
        case TP_ALGO_EVT_TYPE_DOWN:
        gui_fling_on_touch_press(mp_exercise_fling);
        break;

        case TP_ALGO_EVT_TYPE_UP:
        gui_fling_on_touch_release(mp_exercise_fling);
        break;

        case TP_ALGO_EVT_TYPE_HOLD:
        if (touch->deltaY != 0)
        {
            gui_fling_set_roll(mp_exercise_fling, touch->deltaY);
        }
        break;

        case TP_ALGO_EVT_TYPE_SLIDE_D:
        gui_fling_switch_item(mp_exercise_fling, 2, false);
        break;

        case TP_ALGO_EVT_TYPE_SLIDE_U:
        gui_fling_switch_item(mp_exercise_fling, 2, true);
        break;

        case TP_ALGO_EVT_TYPE_SLIDE_L: // watchface 
        local_view_event_handler( tagExerciseRecordTipExit );
        break;

        case TP_ALGO_EVT_TYPE_SLIDE_R: // exit ??
        local_view_event_handler( tagExerciseRecordLastView );
        break;
                
        default:
        break;
    }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    (void)pvParameters; 
}

/* 界面的自定义输入事件处理

*/
static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    (void)pvParameters;
}

/* 界面的触摸事件
   tag 是生效的控件id, 用户根据生效的tag判断是哪个控件点击了，进行处理
*/
static void local_view_event_handler( uint32_t tag )
{
    switch( tag )
    {
        case tagExerciseRecordLastView:
        gui_view_create( views_load_current_watchface() );
        break;

        case tagExerciseRecordTipExit:
        gui_view_create_sub( views_load_exercise_record(), &viewExercise_Tips );
        break;
    }
}
